<?php
namespace app\common\process;

class Poster extends Base {
	protected $画布 = null;
	protected $域名 = '';
	protected $任务 = [];

	public function __construct() {
		$this->生成域名();
	}

    public function get_code($user) {
        $request = loader()->loadAny('request', 'Poster');
        $data = $request->get_code_data();
        extract($data);
        
        $scene = 'uid__' . $user['id'];
        try {

            $code = loadTool('Wechat')->get_code($path, $scene);
            $save_path = 'poster/' . $name . '_' . $user['id'] . '.png';
            $image = imageCreatetruecolor(500, 500); //创建画布

            if($update || !is_file($save_path))
            {
                if(gettype($code) == 'array'){
                    if(isset($code['errmsg']) || !isset($code['type'])) {
                        throw new \Exception($code['errmsg']);
                    }
                } else if(gettype($code) == 'object') {
                    $code = base64_encode($code->data[0]);
                } else if(gettype($code) != 'string') {
                    \think\Log::record($code);
                    throw new \Exception('code异常');
                }
                if(json_decode($code) != null) {
                    \think\Log::record($code);
                    $result = json_decode($code, true);
                    throw new \Exception($result['errmsg']);
                }


                if(gettype($code) == 'string') {
                    $image_qrcode = imagecreatefromstring($code); //读取图片资源
                } else if($code['type'] == 'png') {
                    $image_qrcode = imagecreatefrompng($code['url']);
                }
                imagecopyresampled($image,$image_qrcode,0,0,0,0,500,500,imagesx($image_qrcode),imagesy($image_qrcode));

                imagejpeg($image,  $save_path); //保存图片
                imagedestroy($image);//释放内存
                $this->assign('update', true);
            } else {
                $this->assign('update', false);
            }

            $this->assign('userid', $user['id']);
            $this->assign('url', '/' . $save_path);
            $this->assign('fullurl', $this->域名 . '/' . $save_path);
        } catch(\Exception $e) {
            $this->error($e->getMessage());
        }
        return $this->fetch();
    }

	public function 生成域名() {
		$this->域名 = loader()->loadAny('request', 'Poster')->domain();
	}

	public function 开始作画($用户编号) {
		$公共地址 = str_replace('app/controller/api', 'public', __DIR__);
		$画作地址 = '/haibao/fenxiang_haibao' . '_' . $用户编号 . '.png';
		$画布图片地址 = '/haibao/haibaobeijingtu.jpg';
		$画作服务器地址 = $公共地址 . $画作地址;
		if(is_file($画作服务器地址)) {
			$url = $this->域名 . $画作地址;
			$this->assign('url', $url);
			$this->assign('user_id', $用户编号);
			return this;
		}

		$画布图片地址 = $公共地址 . $画布图片地址;
		if(!is_file($画布图片地址))
        {
            return $this->error('找不到画布图片');
        }

        $this->画布 = imagecreatefromjpeg($画布图片地址);

        $this->执行任务($this->画布);

        imagejpeg($this->画布,  $画作服务器地址); //保存图片
        imagedestroy($this->画布);//释放内存

        $url = str_replace('http://', 'https://', $this->域名) . $画作地址;
        $this->assign('url', $url);
		$this->assign('user_id', $用户编号);
		return $this;
	}

	public function 邀请赚钱() {
		$this->任务[] = '邀请赚钱';
		return $this;
	}

	public function 执行任务($画布) {
		$任务 = $this->$任务;
		foreach($任务 as $编号 => $任务名称) {
			$当前任务 = $任务名称 . '执行任务';
			$this->$当前任务($画布);
		}
		return $this;
	}

	public function 急急如谕令() {
		return $this->fetch();		
	}

	public function temp() {
		


        $user = $this->request->userInfo();
        $info = $this->repository->query(['uid' => $user['uid']])->find();
        $user_id = $user['uid'];

        
       
        $background_img = $dir . '/poster/bg.jpg';

        if(!is_file($background_img))
        {
            return $this->error('暂未设置背景');
        }

        $photo_url = '/poster/share_poster' . '_' . $user_id . '.png';

        $user_img = $dir . $photo_url;
        if(false && is_file($user_img))
        {   
            $url = $domain . $photo_url;
            $this->success('', ['base64' => $url, 'user_id' => $user_id]);
        }

        $image_1 = imagecreatefromjpeg($background_img); //背景图片
        $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1)); //创建画布
        $color = imagecolorallocate($image_3, 255, 255, 255); //设置画布背景色
        imagefill($image_3, 0, 0, $color);
        //把背景图片放到画布里面
        imagecopyresampled($image_3,$image_1,0,0,0,0,imagesx($image_1),imagesy($image_1),imagesx($image_1),imagesy($image_1));

        $code = '';
        $code = $this->get_code();
        if(gettype($code) == 'array'){
            if(isset($code['errmsg']) || !isset($code['type'])) {
                throw new ValidateException('请稍后重试');
            }
        } else if(gettype($code) == 'object') {
            $code = base64_encode($code->data[0]);
        } else if(gettype($code) != 'string') {
            var_dump($code);
        }
        if(json_decode($code) != null) {
            throw new ValidateException($code);
        }

        try {
            if(gettype($code) == 'string') {
                 $image_qrcode = imagecreatefromstring($code); //读取图片资源
             } else if($code['type'] == 'png') {
                $image_qrcode = imagecreatefrompng($code['url']);
             }
           
        }  catch(Exception $e) {
            $this->error($code);
        }

        imagecopyresampled($image_3,$image_qrcode,1060,1865,0,0,400,400,imagesx($image_qrcode),imagesy($image_qrcode));//合成小程序二维码

        $fontSize = 42;//像素字体
        $font = $dir . '/font/PINGFANG_REGULAR.TTF';
        $fontColor = imagecolorallocate ( $image_3, 0, 0, 0 );//字的RGB颜色
        imagettftext( $image_3, $fontSize, 0, 1098, 2350, $fontColor, $font, $info['name'] .'邀请您');
        $remoteurl_ = $dir . $photo_url;
        imagejpeg($image_3,  $remoteurl_); //保存图片
        imagedestroy($image_3);//释放内存

        $domain = str_replace('http://', 'https://', $domain);
        $base64 = $domain . $photo_url;

        $result = ['base64' => $base64, 'user_id' => $user_id];
        return app('json')->success($result);
	}
}
